package cn.bigcontainer.alt.workflow.dto;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.annotation.JsonIgnore;

import cn.bigcontainer.alt.common.po.Po;
import cn.bigcontainer.alt.common.util.WoKit;
import cn.bigcontainer.alt.common.util.WoVelocityKit;
import cn.bigcontainer.alt.meta.dto.MViewDto;
import cn.bigcontainer.alt.workflow.extension.TaskCandidateRouter;
import cn.bigcontainer.alt.workflow.extension.TaskRouter;
import cn.bigcontainer.alt.workflow.mapper.MTaskInstanceMapper;
import cn.bigcontainer.alt.workflow.po.MTaskDefinition;
import cn.bigcontainer.alt.workflow.po.MTaskInstance;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MTaskDefinitionDto extends MTaskDefinition {

    public MTaskDefinitionDto(Po po) {
        super(po);
    }

    public MTaskDefinitionDto() {
    }

    private MViewDto formView;

    public MViewDto getFormView() {
        return formView;
    }

    public void setFormView(MViewDto formView) {
        this.formView = formView;
    }

    /**
     * 获取任务表单提交时的主键值，即为流程实例ID
     * @return
     */
    @JsonIgnore
    public String getProcessInstanceId () {
        return this.getFormView().getKeyValue().toString();
    }

    @JsonIgnore
    public TaskRouter getTaskRouterBean () {
        TaskRouter taskRouter = null;
        if (WoKit.isEmpty(this.getTaskRouter())) {
            taskRouter = WoKit.getBean(TaskRouter.class);
        } else {
            taskRouter = WoKit.getBean(TaskRouter.class, this.getTaskRouter());
        }
        return taskRouter;
    }

    @JsonIgnore
    public TaskCandidateRouter getTaskCandidateRouterBean () {
        TaskCandidateRouter taskCandidateRouterRouter = null;
        if (WoKit.isEmpty(this.getTaskCandidateRouter())) {
            taskCandidateRouterRouter = WoKit.getBean(TaskCandidateRouter.class);
        } else {
            taskCandidateRouterRouter = WoKit.getBean(TaskCandidateRouter.class, this.getTaskCandidateRouter());
        }
        return taskCandidateRouterRouter;
    }

    @JsonIgnore
    public String parseExpression (MTaskDefinitionDto current, String expression) {
        Map<String, Object> data = getFormView().getApp().getObject().getRequest();
        WoKit.getBean(MTaskInstanceMapper.class).selectList(new QueryWrapper<MTaskInstance>().eq("process_instance_id", current.getProcessInstanceId ()).eq("process_definition_id", this.getProcessDefinitionId()).orderByAsc("create_time")).stream().forEach(ti -> data.putAll(ti.getFormValues()));
        return WoKit.getBean(WoVelocityKit.class).parse(expression, data);
    }

    @JsonIgnore
    public String parseTaskExpression () {
        return parseExpression(this, this.getTaskExpression());
    }

    @JsonIgnore
    public List<String> parseTaskCandidateExpression (MTaskDefinitionDto current) {
        String candidates = parseExpression(current, this.getTaskCandidateExpression());
        return Arrays.asList(candidates.split("[ ]*,[ ]*"));
    }
}
